import Vue from "vue";
import Router from "vue-router";
import Layout from "@/pages/layout/layout.vue";
import Login from "@/pages/login";
import nopage from "@/pages/nopage/nopage";

Vue.use(Router);

var router = new Router({
  mode: 'history',
  routes: [
    // 刚进入页面时，进入homePage页面
    // {
    //   path: "/",
    //   redirect: "/homePage"
    // },
    {
      path: "/",
      redirect: "/login"
    },
    // 输入不匹配此页面中定义的路由时，跳转到homePage页面
    // 防止在导航栏随便输入，进入空白页，这里重定向到homePage页面
    {
      path: "*",
      redirect: "/nopage"
    },
    {
      path: "/login",
      name: "login",
      component: Login
    },
    // 侧边栏 首页 路由地址
    {
      path: "/",
      name: "Layout",
      component: Layout,
      children: [
        {
          path: "homePage",
          name: "homePage",
          component: () => import("@/pages/home/homePage"),
          meta: {
            title: "人事管理系统"
          }
        }
      ]
    },
    {
      path: "/",
      name: "Layout",
      component: Layout,
      children: [
        {
          path: "/sys/user",
          name: "user",
          component: () => import("@/pages/sys/user")
        }
      ]
    },
    {
      path: "/",
      name: "Layout",
      component: Layout,
      children: [
        {
          path: "/sys/role",
          name: "role",
          component: () => import("@/pages/sys/role")
        }
      ]
    },
    {
      path: "/",
      name: "Layout",
      component: Layout,
      children: [
        {
          path: "/nopage",
          name: "nopage",
          component: () => import("@/pages/nopage/nopage")
        }
      ]
    },
    {
      path: "/",
      name: "Layout",
      component: Layout,
      children: [
        {
          path: "/sys/permission",
          name: "permission",
          component: () => import("@/pages/sys/permission")
        }
      ]
    }, {
      path: "/",
      name: "Layout",
      component: Layout,
      children: [
        {
          path: "/sys/dept",
          name: "dept",
          component: () => import("@/pages/sys/dept")
        }
      ]
    }, {
      path: "/",
      name: "Layout",
      component: Layout,
      children: [
        {
          path: "/personnel/employee",
          name: "employee",
          component: () => import("@/pages/personnel/employee")
        }
      ]
    }, {
      path: "/",
      name: "Layout",
      component: Layout,
      children: [
        {
          path: "/personnel/attendance",
          name: "attendance",
          component: () => import("@/pages/personnel/attendance")
        }
      ]
    }, {
      path: "/",
      name: "Layout",
      component: Layout,
      children: [
        {
          path: "/personnel/salary",
          name: "salary",
          component: () => import("@/pages/personnel/salary")
        }
      ]
    }, {
      path: "/",
      name: "Layout",
      component: Layout,
      children: [
        {
          path: "/sys/log",
          name: "log",
          component: () => import("@/pages/sys/log")
        }
      ]
    }, {
      path: "/",
      name: "Layout",
      component: Layout,
      children: [
        {
          path: "/personnel/training",
          name: "training",
          component: () => import("@/pages/personnel/training")
        }
      ]
    }, {
      path: "/",
      name: "Layout",
      component: Layout,
      children: [
        {
          path: "/personnel/rewardandpunishment",
          name: "rewardandpunishment",
          component: () => import("@/pages/personnel/rewardandpunishment")
        }
      ]
    }, {
      path: "/",
      name: "Layout",
      component: Layout,
      children: [
        {
          path: "/personnel/check",
          name: "check",
          component: () => import("@/pages/personnel/check")
        }
      ]
    }, {
      path: "/",
      name: "Layout",
      component: Layout,
      children: [
        {
          path: "/personnel/change",
          name: "change",
          component: () => import("@/pages/personnel/change")
        }
      ]
    }
  ]
});

//解决重复点击路由时，控制台报错
//获取原型对象上的push函数
const originalPush = Router.prototype.push;
//修改原型对象中的push方法
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};

// router.beforeEach((to, from, next) => {
// })

// 全局前置守卫
// 当一个导航触发时，全局前置守卫按照创建顺序调用。
// 守卫是异步解析执行，此时导航在所有守卫 resolve 完之前一直处于 等待中
router.beforeEach((to, from, next) => {
  // 想办法获取token，一般都是存储在vuex中
  let isLogin = window.localStorage.getItem("token");
  // 存在token就让他登录，跳转到登录页写好的跳转地址
  if (isLogin) {
    next();
  }
  // 否则就让他跳转到登录页
  else {
    if (to.path === "/login") {
      next();
    } else {
      let v = new Vue();
      v.$message.error('没有访问权限或登录已过期，请重新登录！');
      next("/login");
    }
  }
});

export default router;
